package com.joker.aigc.chat.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketBaseVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketChatMemberStatisticVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketChatMemberVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketFriendApplyVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketLoginSuccessVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketLoginUrlVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMessageSendVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMsgRecallVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketOnlineOfflineNotifyVo;
import com.joker.aigc.chat.domain.enums.WebSocketVoTypeEnum;
import com.joker.aigc.domain.entity.User;
import com.joker.aigc.domain.enums.WebSocketChatActiveStatusEnum;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * Description: ws消息适配器
 * Author: <a href="https://github.com/zongzibinbin">abin</a>
 * Date: 2023-03-19
 */
@Component
public class WebSocketAdapter {


//    @Autowired
//    private ChatService chatService;

    public static WebSocketBaseVo<WebSocketLoginUrlVo> buildLoginResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        // 构造 登录信息
        WebSocketBaseVo<WebSocketLoginUrlVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.LOGIN_URL.getType());
        WebSocketBaseVo.setData(WebSocketLoginUrlVo.builder().loginUrl(wxMpQrCodeTicket.getUrl()).build());
        return WebSocketBaseVo;
    }

    public static WebSocketBaseVo<WebSocketLoginSuccessVo> buildLoginSuccessResp(
            User user, String token, boolean hasPower) {
        // 构建成功登录的消息
        WebSocketBaseVo<WebSocketLoginSuccessVo> webSocketBaseVo = new WebSocketBaseVo<>();
        webSocketBaseVo.setType(WebSocketVoTypeEnum.LOGIN_SUCCESS.getType());
        WebSocketLoginSuccessVo wsLoginSuccess = WebSocketLoginSuccessVo.builder()
                .avatar(user.getAvatar())
                .name(user.getUsername())
                .token(token)
                .uid(user.getId())
                .power(hasPower ? 1 : 0)
                .build();
        webSocketBaseVo.setData(wsLoginSuccess);
        return webSocketBaseVo;
    }

//    public static WebSocketBaseResp buildScanSuccessResp() {
//        WebSocketBaseResp WebSocketBaseResp = new WebSocketBaseResp();
//        WebSocketBaseResp.setType(WebSocketRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
//        return WebSocketBaseResp;
//    }

    public static WebSocketBaseVo<?> buildMsgRecall(WebSocketMsgRecallVo recallDTO) {
        // 构建返回消息
        WebSocketBaseVo<WebSocketMsgRecallVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.RECALL.getType());
        WebSocketMsgRecallVo recall = new WebSocketMsgRecallVo();
        BeanUtils.copyProperties(recallDTO, recall);
        WebSocketBaseVo.setData(recall);
        return WebSocketBaseVo;
    }

    public static  WebSocketBaseVo<WebSocketOnlineOfflineNotifyVo> buildOnlineNotifyResp(User user) {
        WebSocketBaseVo<WebSocketOnlineOfflineNotifyVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        WebSocketOnlineOfflineNotifyVo onlineOfflineNotify = new WebSocketOnlineOfflineNotifyVo();
        onlineOfflineNotify.setChangeList(Collections.singletonList(buildOnlineInfo(user)));
//        assembleNum(onlineOfflineNotify);
        WebSocketBaseVo.setData(onlineOfflineNotify);
        return WebSocketBaseVo;
    }

    public static WebSocketBaseVo<WebSocketOnlineOfflineNotifyVo> buildOfflineNotifyResp(User user) {
        /**
         * 构建下线消息
         */
        WebSocketBaseVo<WebSocketOnlineOfflineNotifyVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        WebSocketOnlineOfflineNotifyVo onlineOfflineNotify = new WebSocketOnlineOfflineNotifyVo();
        onlineOfflineNotify.setChangeList(Collections.singletonList(buildOfflineInfo(user)));
        assembleNum(onlineOfflineNotify);
        WebSocketBaseVo.setData(onlineOfflineNotify);
        return WebSocketBaseVo;
    }

    private static void assembleNum(WebSocketOnlineOfflineNotifyVo onlineOfflineNotify) {
//        WebSocketChatMemberStatisticResp memberStatistic = chatService.getMemberStatistic();
        WebSocketChatMemberStatisticVo memberStatistic = new WebSocketChatMemberStatisticVo();
        memberStatistic.setOnlineNum(0L);
        onlineOfflineNotify.setOnlineNum(memberStatistic.getOnlineNum());
    }

    private static WebSocketChatMemberVo buildOnlineInfo(User user) {
        // 构造上线消息
        WebSocketChatMemberVo info = new WebSocketChatMemberVo();
        BeanUtil.copyProperties(user, info);
        info.setUid(user.getId());
        info.setActiveStatus(WebSocketChatActiveStatusEnum.ONLINE.getStatus());
        info.setLastOptTime(user.getLastOptTime());
        return info;
    }

    private static WebSocketChatMemberVo buildOfflineInfo(User user) {
        // 构造下线消息
        WebSocketChatMemberVo info = new WebSocketChatMemberVo();
        BeanUtil.copyProperties(user, info);
        info.setUid(user.getId());
        info.setActiveStatus(WebSocketChatActiveStatusEnum.OFFLINE.getStatus());
        info.setLastOptTime(user.getLastOptTime());
        return info;
    }

    public static WebSocketBaseVo<WebSocketLoginSuccessVo> buildInvalidateTokenResp() {
        WebSocketBaseVo<WebSocketLoginSuccessVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.INVALIDATE_TOKEN.getType());
        return WebSocketBaseVo;
    }

    public static WebSocketBaseVo<WebSocketMessageSendVo> buildMsgSend(WebSocketMessageSendVo msgResp) {
        WebSocketBaseVo<WebSocketMessageSendVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.MESSAGE.getType());
        WebSocketBaseVo.setData(msgResp);
        return WebSocketBaseVo;
    }

//    public static WebSocketBaseResp<WebSocketMsgMark> buildMsgMarkSend(ChatMessageMarkDTO dto, Integer markCount) {
//        WSMsgMark.WSMsgMarkItem item = new WSMsgMark.WSMsgMarkItem();
//        BeanUtils.copyProperties(dto, item);
//        item.setMarkCount(markCount);
//        WebSocketBaseResp<WSMsgMark> WebSocketBaseResp = new WebSocketBaseResp<>();
//        WebSocketBaseResp.setType(WebSocketRespTypeEnum.MARK.getType());
//        WSMsgMark mark = new WSMsgMark();
//        mark.setMarkList(Collections.singletonList(item));
//        WebSocketBaseResp.setData(mark);
//        return WebSocketBaseResp;
//    }

    public static WebSocketBaseVo<WebSocketFriendApplyVo> buildApplySend(WebSocketFriendApplyVo resp) {
        WebSocketBaseVo<WebSocketFriendApplyVo> WebSocketBaseVo = new WebSocketBaseVo<>();
        WebSocketBaseVo.setType(WebSocketVoTypeEnum.APPLY.getType());
        WebSocketBaseVo.setData(resp);
        return WebSocketBaseVo;
    }
}
